package com.umeng.community.example;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.qianxx.base.BaseActivity;
import com.qianxx.base.utils.SPUtil;
import com.qianxx.base.widget.Indicator;
import com.qianxx.draw.R;

import java.util.ArrayList;

/**
 * 引导页： 以及海报
 * */
public class GuideAty extends BaseActivity {

	private ArrayList<View> list = new ArrayList<View>(); // 用于存储ViewPager关联的view
	private ViewPager mPager;
	private Indicator mIndicator;
	private View btnSkip;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.aty_guide);
		btnSkip = findViewById(R.id.btnSkip);
		initList();
		initViewPager();
		initIndicator();
	}

	private void initList() {
		LayoutInflater inflater = getLayoutInflater();
		int[] resId = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a3};
		list.clear();
		for (int i = 0; i < resId.length; i++) {
			View view = inflater.inflate(R.layout.layout_guide_item, null);
			ImageView imgItem = (ImageView) view.findViewById(R.id.imgItem);
			imgItem.setImageResource(resId[i]);
			list.add(view);
		}
	}

	private void initViewPager() {
		mPager = (ViewPager) findViewById(R.id.viewPager);
		mPager.setAdapter(new PagerAdapter() {

			@Override
			public Object instantiateItem(ViewGroup container, int position) {
				View itemView = list.get(position);
				mPager.addView(itemView);
				return itemView;
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				View itemView = list.get(position);
				mPager.removeView(itemView);
			}

			@Override
			public int getCount() {
				return list.size();
			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

		});
	}

	private void initIndicator() {
		mIndicator = (Indicator) findViewById(R.id.indicator);
		mIndicator.setCount(list.size());
		mPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int pos) {
				if (pos == (list.size() - 1)) {
					btnSkip.setVisibility(View.VISIBLE);
				} else {
					btnSkip.setVisibility(View.INVISIBLE);
				}
			}

			@Override
			public void onPageScrolled(int pos, float arg1, int arg2) {
				mIndicator.move(pos, arg1);
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}

	// 跳转主页
	public void skipToMain(View v) {
		Intent intent = new Intent(GuideAty.this, MainActivity.class);
		startActivity(intent);
		SPUtil.getInstance().setFirst(false);
		finish();
		
	}

}
